from tkinter import *
from tkinter.ttk import Separator


class PlacementAlgorithm:
    pass


def show_reconstruct_input_window(input):
    from collections import abc
    algorithm_list = ['economic_common_centroid_multi_rows', 'economic_common_centroid_one_row',
                      'capacitor_placement']
    win = Toplevel()
    win.title("Reconstuct")

    cap_merge_type_dict = {'MERGE_ALL': 0, 'MERGE_MINUS': 1, 'MERGE_PLUS': 2, 'MERGE_NONE': 3}
    algorithm_attributes = {
        "simple_placement_no_matching_multi_rows": {"rows": IntVar(),
                                                    "columns": IntVar()},
        "resistor_placement": {"rows": IntVar(), "columns": IntVar()},
        "economic_common_centroid_multi_rows": {"rows": IntVar(), "columns": IntVar()},
        "capacitor_placement": {"rows": IntVar(), "columns": IntVar(), "merge_type": cap_merge_type_dict}
    }
    algorithm_select = StringVar()
    algorithm_select.set(algorithm_list[0])
    global frame_row
    common_frame_font = {'relief': FLAT}
    common_frame_grid = {'ipadx': 5, 'ipady': 1, 'padx': 1, 'pady': 2, 'sticky': W}

    def refresh_algorithm_prop_frame():
        row = 0
        print(row)
        select_algorithm = algorithm_select.get()
        for item in frame_algorithm_prop.winfo_children():
            item.destroy()
        frame_algorithm_prop.configure(height=1)
        if select_algorithm in algorithm_attributes:
            for prop, v in algorithm_attributes[select_algorithm].items():
                Label(frame_algorithm_prop, text=prop).grid(row=row, column=0, sticky=W)
                if not isinstance(v, abc.Mapping):
                    Entry(frame_algorithm_prop, textvariable=algorithm_attributes[select_algorithm][prop]).grid(
                        row=row, column=1, sticky=W)
                elif isinstance(v, dict):
                    selected = IntVar()
                    selected.set(0)
                    for s_prop, s_v in v.items():
                        Radiobutton(frame_algorithm_prop, text=s_prop, variable=selected, value=s_v).grid(row=row,
                                                                                                          column=1,
                                                                                                          sticky=W)
                        row += 1
                row += 1

    def add_separator():
        global frame_row
        frame_row += 1
        seq = Separator(win, orient=HORIZONTAL, style='red.TSeparator')
        seq.grid(row=frame_row, column=0, sticky='EW', pady=5, padx=5)

    row = 0
    frame_row = 0
    frame_info = LabelFrame(win, text='component info', **common_frame_font)
    frame_info.grid(row=frame_row, column=0, **common_frame_grid)
    Label(frame_info, text='library').grid(column=0, row=row, sticky=W)
    Entry(frame_info).grid(column=1, row=row, sticky=W)
    row += 1
    Label(frame_info, text='device model').grid(column=0, row=row, sticky=W)
    Entry(frame_info).grid(column=1, row=row, sticky=W)

    add_separator()
    row = 0
    frame_row += 1
    frame_algorithm = LabelFrame(win, **common_frame_font, text=' placement algorithm')
    frame_algorithm.grid(row=frame_row, column=0, **common_frame_grid)
    for algorithm in algorithm_list:
        Radiobutton(frame_algorithm, text=algorithm, variable=algorithm_select, value=algorithm,
                    command=refresh_algorithm_prop_frame).grid(row=row, column=0,
                                                               sticky=W)
        row += 1

    add_separator()
    frame_row += 1
    frame_algorithm_prop = LabelFrame(win, **common_frame_font, text='algorithm property')
    frame_algorithm_prop.grid(row=frame_row, column=0, **common_frame_grid)
    refresh_algorithm_prop_frame()

    add_separator()
    frame_row += 1
    row = 0
    frame_other = LabelFrame(win, **common_frame_font, text='')
    frame_other.grid(row=frame_row, column=0, **common_frame_grid)
    Label(frame_other, text='Length').grid(column=0, row=row, sticky=W)
    Entry(frame_other).grid(column=1, row=row, sticky=W)
    row += 1
    Label(frame_other, text='Finger Width').grid(column=0, row=row, sticky=W)
    Entry(frame_other).grid(column=1, row=row, sticky=W)
    row += 1
    Label(frame_other, text='Fingers').grid(column=0, row=row, sticky=W)
    Entry(frame_other).grid(column=1, row=row, sticky=W)
    row += 1
    Label(frame_other, text='Total Width').grid(column=0, row=row, sticky=W)
    Entry(frame_other).grid(column=1, row=row, sticky=W)

    row += 1
    sd_merge = IntVar()
    sd_merge.set(0)
    Label(frame_other, text='S/D Connect').grid(column=0, row=row, sticky=W)
    Radiobutton(frame_other, text="True", value=1, variable=sd_merge).grid(row=row, column=1)
    Radiobutton(frame_other, text="False", value=0, variable=sd_merge).grid(row=row, column=2)

    row += 1

    add_dummy = IntVar()
    add_dummy.set(0)
    Label(frame_other, text='Add Dummy').grid(column=0, row=row, sticky=W)
    Radiobutton(frame_other, text="True", value=1, variable=add_dummy).grid(row=row, column=1)
    Radiobutton(frame_other, text="False", value=0, variable=add_dummy).grid(row=row, column=2)

    win.mainloop()


if __name__ == '__main__':
    show_reconstruct_input_window(1)
